<template>
    <div class="login">
        <!-- 导航 -->
        <div class="nav-bar">
            <van-nav-bar>
                <template #left>
                    <div class="nav-logo">
                        <div class=""><img src="../assets/images/home_active.png" alt=""></div>
                        <div>Luckin&nbsp;Coffee</div>
                    </div>
                </template>
                 <template #right>
                     <div class="nav-right" @click="goback">先逛一逛</div>
                </template>
            </van-nav-bar>
        </div>
        <!-- 欢迎回来 -->
        <div class="welcome">
            <div class="ch-wel">欢迎回来</div>
            <div class="en-wel">Please login to your accounts</div>
        </div>
        <!-- 登录 -->
        <div class="form-box">
            <van-form @submit="login">
                    <van-field
                        v-model="userInfo.phone"
                        name="phone"
                        label="手机号"
                        placeholder="手机号"
                        :rules="[{ required: true, message: '请填写手机号' },{pattern:phoneReg,message:'手机号格式不正确'}]"
                    />
                    <van-field
                        v-model="userInfo.password"
                        :type="isOpen?'text':'password'"
                        name="password"
                        label="密码"
                        placeholder="密码"
                        :right-icon="isOpen?'eye-o':'closed-eye'"
                        @click-right-icon="togglePasswordStatus"
                        autocomplete="off"
                        :rules="[{ required: true, message: '请填写密码' },{pattern:passwordReg,message:'以字母开头的数字、字母、下划线组成6-16位密码'}]"
                    />
                    <div class="forgot-box">
                        <div class="forgot-text" @click="toForgetpassword">
                            忘记密码？
                        </div>
                    </div>
                    <div style="margin: 16px;">
                        <van-button round block color="#0C34BA" native-type="submit" >登&nbsp;&nbsp;&nbsp;录</van-button>
                    </div>
                     <div style="margin: 16px;">
                        <van-button round block native-type="button" @click='toRegister'>注&nbsp;&nbsp;&nbsp;册</van-button>
                    </div>
            </van-form>
        </div>
    </div>
</template>

<script>
    export default {
        data() {
            return {
                isOpen:false,
                 // 验证手机号
                phoneReg:/^1[3-9]\d{9}$/,
                // 验证密码（以字母开头的数字、字母、下划线组成6-16位密码）
                passwordReg:/^[A-Za-z]\w{5,15}$/,
                userInfo:{
                    phone:'',
                    password:'',
                }
            }
        },
        methods: {
            // 点击忘记密码 跳转
            toForgetpassword(){
                this.$router.push({name:'Forgetpassword'})
            },
             // 点击逛一逛
            goback(){
                console.log(11);
                this.$router.push({name:'Home'})
            },
            togglePasswordStatus(){
                this.isOpen=!this.isOpen
            },
            // 点击注册 跳转页面
            toRegister(){
                this.$router.push({name:'Register'})
            },
            // 登录
             login(userInfo){
                console.log('userInfo',userInfo);
                this.axios({
                    method:'post',
                    url:'/login',
                    data:userInfo,
                }).then(res=>{
                    console.log('res=>',res);
                    this.$toast(res.data.msg)
                    if(res.data.code==200){
                        // 将token保存在sessionStorage 
                        sessionStorage.setItem('token',res.data.token)
                        setTimeout(()=>{
                            this.$router.push({name:'Menu'})
                        },2000)
                    }
                }).catch(err=>{
                    console.log( 'err=>',err);
                })
            }
        },
    }
</script>

<style lang="less" scoped>
// 登录
.form-box{
    margin-top: 50px;
    // 忘记密码
    .forgot-box{
        margin-top: 10px;
        display: flex;
        .forgot-text{
            margin-left: auto;
            font-size: 14px;
            color: #666;
        }
    }
}
// 欢迎回来
.welcome{
    margin-top: 80px;
    padding: 10px;
    .ch-wel{
        font-size: 26px;
        color: #666;
        font-weight: bold;
    }
    .en-wel{
        font-size: 14px;
        color: #666;
        margin-top: 20px;
    }
}
// 导航
.nav-bar{
    .nav-logo{
        display: flex;
        align-items: center;
        div{
            flex: 1;
            font-size: 16px;
            color: #666;
            font-weight: bold;
            img{
                width: 40px;
                height: 40px;
                border-radius: 50%;
                margin-right: 10px;
            }
        }
    }
    .nav-right{
        color: #0C34BA;
        font-size: 16px;
        font-weight: bold;
    }
}
</style>